# -*- coding: utf-8 -*-
# @Time    : 2024/12/9  15:49
# @Author  : Nabby
# @FileName: jasco_forecast_information_impl.py
# @Desc    :
from app import db
from infrastructure.po.mode.jasco_received_information import JascoReceivedInformation
from sqlalchemy import and_, or_, desc, asc


class JascoReceivedInformationImpl(object):

    @classmethod
    def get_received_information_list(cls, order_id, page=1, page_size=999):
        query = db.session.query(JascoReceivedInformation).filter(
            JascoReceivedInformation.order_id == order_id
        )
        return (
            query.order_by(JascoReceivedInformation.create_time.desc()).paginate(
                page=page, per_page=page_size
            ),
            query.count(),
        )

    @classmethod
    def add_received_information(
        cls, order_id, cargo_code, length, width, height, actual_weight, volume, volumetric_weight, billing_weight, billing_squaremeter, sub_bill_code, transportation_status
    ):
        count = (
            db.session.query(JascoReceivedInformation)
            .filter(
                and_(
                    JascoReceivedInformation.cargo_code == cargo_code,
                    JascoReceivedInformation.order_id == order_id,
                )
            )
            .count()
        )
        if count > 0:
            return False
        else:
            jasco_received_information = JascoReceivedInformation(
                cargo_code=cargo_code,
                length=length,
                width=width,
                height=height,
                actual_weight=actual_weight,
                volume=volume,
                volumetric_weight=volumetric_weight,
                billing_weight=billing_weight,
                billing_squaremeter=billing_squaremeter,
                sub_bill_code=sub_bill_code,
                transportation_status=transportation_status,
                order_id=order_id,
            )
            db.session.add(jasco_received_information)
            db.session.flush()
            db.session.commit()
            return True

    @classmethod
    def edit_received_information(cls, id, order_id, cargo_code, length, width, height, actual_weight, volume, volumetric_weight, billing_weight, billing_squaremeter, sub_bill_code, transportation_status):
        query = db.session.query(JascoReceivedInformation).filter(and_(JascoReceivedInformation.id == id, JascoReceivedInformation.order_id == order_id))
        if query.count() == 0:
            return False
        else:
            jasco_received_information = query.first()
            jasco_received_information.cargo_code = cargo_code
            jasco_received_information.length = length
            jasco_received_information.width = width
            jasco_received_information.height = height
            jasco_received_information.actual_weight = actual_weight
            jasco_received_information.volume = volume
            jasco_received_information.volumetric_weight = volumetric_weight
            jasco_received_information.billing_weight = billing_weight
            jasco_received_information.billing_squaremeter = billing_squaremeter
            jasco_received_information.sub_bill_code = sub_bill_code
            jasco_received_information.transportation_status = transportation_status
            db.session.flush()
            db.session.commit()
            return True

    @classmethod
    def delete_received_information(cls, id, order_id):
        query = db.session.query(JascoReceivedInformation).filter(and_(JascoReceivedInformation.id == id, JascoReceivedInformation.order_id == order_id))
        if query.count() == 0:
            return False
        else:
            query.delete()
            db.session.flush()
            db.session.commit()
            return True
